//
//  IdentityVerifyViewController.m
//  HelpThePoor
//
//  Created by 韩中睿 on 2017/7/20.
//  Copyright © 2017年 van7ish. All rights reserved.
//

#import "IdentityVerifyViewController.h"

@interface IdentityVerifyViewController ()

/** <#注释#> */
@property (nonatomic, strong) UIImageView * headerImageView;
/** <#注释#> */
@property (nonatomic, strong) UIButton * backButton;

/** <#注释#> */
@property (nonatomic, strong) UIButton * confirmButton;
/** <#注释#> */
@property (nonatomic, strong) UIView * registWayID;
/** <#注释#> */
@property (nonatomic, strong) UIView * nameView;
/** <#注释#> */
@property (nonatomic, strong) UIView * idView;
/** <#注释#> */
@property (nonatomic, strong) UIView * genderView;

/** 姓名输入框 */
@property (nonatomic, strong)  UITextField * nameTextField;
/** <#注释#> */
@property (nonatomic, strong) UITextField * idTextField;
/** <#注释#> */
@property (nonatomic, strong) NSString * genderStr;
/** <#注释#> */
@property (nonatomic, strong) UIView * overView;


@end

@implementation IdentityVerifyViewController

- (UIView *)overView{
    if (!_overView) {
        _overView = [[UIView alloc] initWithFrame:CGRectMake(0, self.headerImageView.bottom, SCREEN_W, HeightScale(200))];
        _overView.backgroundColor = COLOR_LIGHTBGCOLOR;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_W - WidthScale(32)) / 2, HeightScale(100), WidthScale(32), WidthScale(32))];
        imageView.image = ImageNamed(@"Done-1");
        [_overView addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.bottom + HeightScale(20), SCREEN_W, HeightScale(30))];
        label.font = FONTSIZE(17);
        label.textColor = COLOR_GREEN;
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"已完成实名认证!";
        [_overView addSubview:label];
        _overView.hidden = YES;
    }
    return _overView;
}

- (UIView *)registWayID{
    if (!_registWayID) {
        _registWayID = [[UIView alloc] initWithFrame:CGRectMake(0, self.headerImageView.bottom + HeightScale(20), SCREEN_W, HeightScale(150))];
        
        _nameView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, HeightScale(50))];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(40), HeightScale(23.5), WidthScale(13.5), HeightScale(13))];
        imageView.image = [UIImage imageNamed:@"name"];
        [_nameView addSubview:imageView];
        
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(imageView.right + WidthScale(15), HeightScale(10), SCREEN_W - WidthScale(180), HeightScale(40))];
        _nameTextField = textField;
        textField.placeholder = @"输入真实姓名";
        textField.font = FONTSIZE(17);
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_nameView addSubview:textField];
        [_registWayID addSubview:_nameView];
        UIImageView *underline = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _nameView.bottom - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underline.image = [UIImage imageNamed:@"fengexian"];
        [_nameView addSubview:underline];
        
        
        
        _idView = [[UIView alloc] initWithFrame:CGRectMake(0, _nameView.bottom, SCREEN_W, HeightScale(50))];
        UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(imageView.left, HeightScale(23), WidthScale(16), HeightScale(14))];
        imageView2.image = [UIImage imageNamed:@"card"];
        [_idView addSubview:imageView2];
        
        UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(textField.left, HeightScale(10), textField.width, textField.height)];
        _idTextField = textField2;
        textField2.placeholder = @"输入身份证号";
        textField2.font = FONTSIZE(17);
        textField2.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_idView addSubview:textField2];
        [_registWayID addSubview:_idView];
        UIImageView *underline1 = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _idView.height - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underline1.image = [UIImage imageNamed:@"fengexian"];
        [_idView addSubview:underline1];
        
        
        
        _genderView = [[UIView alloc] initWithFrame:CGRectMake(0, _idView.bottom, SCREEN_W, HeightScale(50))];
        UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(imageView.left, HeightScale(23.5), WidthScale(14.5), HeightScale(13))];
        imageView3.image = [UIImage imageNamed:@"xingbie"];
        [_genderView addSubview:imageView3];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(textField.left, HeightScale(10), WidthScale(60), HeightScale(40))];
        label.text = @"性别";
        label.font = FONTSIZE(17);
        [_genderView addSubview:label];
        
        NSArray *arr = @[@"男", @"女"];
        for (int i = 0; i < 2; i++) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(label.right + i * WidthScale(80), label.top, WidthScale(80), label.height)];
            [_genderView addSubview:button];
            [button setImage:[UIImage imageNamed:@"xingbie_unselected"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"done"] forState:UIControlStateSelected];
            [button setTitle:arr[i] forState:UIControlStateNormal];
            [button setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
            button.titleLabel.font = FONTSIZE(17);
            button.tag = i;
            button.imageEdgeInsets = UIEdgeInsetsMake(0, -WidthScale(3), 0, WidthScale(3));
            button.titleEdgeInsets = UIEdgeInsetsMake(0, WidthScale(3), 0, -WidthScale(3));
            [button initWithBlock:^(UIButton *btn) {
                for (UIView *view in _genderView.subviews) {
                    if ([view isKindOfClass:[UIButton class]]) {
                        UIButton *button = (UIButton *)view;
                        button.selected = NO;
                    }
                }
                btn.selected = YES;
                if (button.tag == 0) {
                    _genderStr = @"1";
                } else {
                    _genderStr = @"2";
                }
                
            } for:UIControlEventTouchUpInside];
        }
        [_registWayID addSubview:_genderView];
        UIImageView *underline2 = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _genderView.height - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underline2.image = [UIImage imageNamed:@"fengexian"];
        [_genderView addSubview:underline2];
    }
    return _registWayID;
}


- (UIButton *)confirmButton{
    if (!_confirmButton) {
        _confirmButton = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_W - WidthScale(80)) / 2, self.view.height - HeightScale(70), WidthScale(80), HeightScale(40))];
        [_confirmButton setTitle:@"确定" forState: UIControlStateNormal];
        [_confirmButton setTitleColor:COLOR_GREEN forState:UIControlStateNormal];
        _confirmButton.titleLabel.font = [UIFont boldSystemFontOfSize:WidthScale(17)];
        
        __weak typeof(self)weakSelf = self;
        [_confirmButton initWithBlock:^(id obj) {
            DLog(@"确定");
            
            if (_nameTextField.text.length == 0) {
                [ZRProgressHUD showMsgWithoutView:@"请输入真实姓名"];
                return ;
            }
            if (![Tool isValidateIDNumber:_idTextField.text]) {
                [ZRProgressHUD showMsgWithoutView:@"非法的身份证号，请重新输入"];
                return;
            }
            if (_genderStr.length == 0) {
                [ZRProgressHUD showMsgWithoutView:@"请选择性别"];
                return;
            }
            
            if (self.overView.hidden) {
                [weakSelf requestVerification];
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
            
            
            
        } for:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}

- (UIButton *)backButton{
    if (!_backButton) {
        _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 44, 44)];
        [_backButton setImage:ImageNamed(@"back") forState:UIControlStateNormal];
        _backButton.transform = CGAffineTransformMakeRotation(-M_PI_2);
        __weak typeof(self)weakSelf = self;
        [_backButton initWithBlock:^(id obj) {
            DLog(@"返回");
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        } for:UIControlEventTouchUpInside];
    }
    return _backButton;
}

- (UIImageView *)headerImageView{
    if (!_headerImageView) {
        _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_W / 2)];
        _headerImageView.image = ImageNamed(@"background");
        _headerImageView.userInteractionEnabled = YES;
    }
    return _headerImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_LIGHTBGCOLOR;
    
    [self.view addSubview:self.headerImageView];
    
    [self.view addSubview:self.backButton];
    
    [self.view addSubview:self.registWayID];
    
    [self.view addSubview:self.confirmButton];
    
    [self.view addSubview:self.overView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 数据请求

- (void)requestVerification{
    NSString *url = [NSString stringWithFormat:KidentityVerify, KHostAddress, [AppDelegate shareDelegate].userModel.userId, _idTextField.text, _nameTextField.text, _genderStr];
    [ZRRequestManager POST:url parameters:nil success:^(id responseObject) {
        DLog(@"%@", responseObject);
        if ([[responseObject objectForKey:@"error"] intValue] == 0) {

            self.overView.hidden = NO;
            
            NSData *userData = [[NSUserDefaults standardUserDefaults] objectForKey:@"user"];
            
            NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithData:userData]];
            [tempDic addEntriesFromDictionary:@{@"idNumber":_idTextField.text, @"actualName":_nameTextField.text}];
            [AppDelegate shareDelegate].userModel = [UserModel modelWithDictionary:tempDic];
            
            //字典序列化并存储到NSUserDefaults
            userData = [NSKeyedArchiver archivedDataWithRootObject:tempDic];
            
            [[NSUserDefaults standardUserDefaults] setObject:userData forKey:@"user"];
            
            
        } else {
            
        }
        
    } failure:^(NSError *error) {
        DLog(@"%@", error);
    }];
}

@end
